﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RipPoll.Web.Mvc.Models;
using RipPoll.Library;
using RipPoll.Common;

namespace RipPoll.Web.Mvc.Controllers
{
    public class PollController : BaseController
    {
        //
        // GET: /Poll/


        public ActionResult PollCommunity()
        {
            return View();
        }

        public ActionResult Preview()
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            PollPE pe = Session[WebConstants.UnSavedPoll] as PollPE;
            PollModel pm = new PollModel();
            pm.Id = pe.Id;
            pm.Name = pe.Name;
            if (pe.Category.Id == 1)
                pm.Category = "Yes/No";
            else
                pm.Category = "Multiple Choice";
            pm.Choices = (from c in pe.Choices
                         select c.Choice).ToList<string>();

            return View(pm);
        }

        [HttpPost()]
        public ActionResult GetZipCodesForPollid(string PollId)
        {
            if (!string.IsNullOrEmpty(PollId) && PollId.Trim() != "0")
            {
                PollServices.PollServicesClient pbo = new PollServices.PollServicesClient();
                string[] zips = pbo.GetZipCodesForPollid(int.Parse(PollId)).ToArray();
                //JsonResult jsr = new JsonResult();
                //jsr.Data = zips;
                return Json(zips);
            }
            else
            {
                //seems unsaved poll
                string[] zips = new string[0];
                return Json(zips);
            }
            
        }

       
        
        public ActionResult Create1()
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            PollPE pollPE = Session[WebConstants.UnSavedPoll] as PollPE;
            return View(pollPE);
        }


        public ActionResult myrippolls()
        {
            UserPE upe = Session[WebConstants.CurrentUser] as UserPE;
            if (upe != null)
            {
                ViewBag.City = upe.City;
                ViewBag.State = upe.State;

                PollServices.PollServicesClient pclient = new PollServices.PollServicesClient();
                List<PollPE> plist = pclient.GetUserPolls(upe.Id);

                return View(plist);
            }
            return View();
        }
        
        
        [HttpPost()]
        public ActionResult Create1(string returnUrl)
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            string cat = Request.Form["radioCategory"];

            PollPE pollPE = new PollPE();
            pollPE.Category = new Library.PollCategoryPE();
            pollPE.Category.DateAdded = DateTime.Now;
            pollPE.Category.Id = int.Parse(cat);
            Session[WebConstants.UnSavedPoll] = pollPE;

            return RedirectToAction("Create2", "Poll");
        }
        
        public ActionResult Create2()
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            UserPE upe = Session[WebConstants.CurrentUser] as UserPE;
            ViewBag.UserCity = upe.City;
            ViewBag.FirstName = upe.FirstName;
            ViewBag.LastName = upe.LastName;
            ViewBag.State = upe.State;

            PollPE ppe = Session[WebConstants.UnSavedPoll] as PollPE;
            
            if (Session[WebConstants.UnSavedPoll] != null)
                ViewBag.PollText = ppe.Name;
            if (string.IsNullOrEmpty(ViewBag.PollText))
                ViewBag.PollText = "Type in your question here. 150 characters max.";
            return View(ppe);
        }


        [HttpPost()]
        public ActionResult Create2(string returnUrl)
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            string question = Request.Form["question"];
            string firstname = Request.Form["firstname"];
            string lastname = Request.Form["lastname"];
            string city = Request.Form["city"];
            string state = Request.Form["state"];

            UserPE upe = Session[WebConstants.CurrentUser] as UserPE;
            if (!string.IsNullOrEmpty(city))
                upe.City = city;
            if (!string.IsNullOrEmpty(firstname))
                upe.FirstName = firstname;
            if (!string.IsNullOrEmpty(lastname))
                upe.LastName = lastname;
            if (!string.IsNullOrEmpty(state))
                upe.State = state;

            UserServices.UserServicesClient ucli = new UserServices.UserServicesClient();
            ucli.UpdateUserForPoll(upe);

            Session[WebConstants.CurrentUser] = upe;
            PollPE pm = Session[WebConstants.UnSavedPoll] as PollPE;
            if (pm == null)
                return RedirectToAction("create1", "Poll");

            pm.Name = question;

            Session[WebConstants.UnSavedPoll] = pm;
            return RedirectToAction("Create3", "Poll");
        }
        
        public ActionResult Create3()
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            PollPE pollpe = Session[WebConstants.UnSavedPoll] as PollPE;
            return View(pollpe);
        }

        [HttpPost()]
        public ActionResult Create3(string returnUrl)
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            string optionVal = Request.Form["rdCh"];
            //string totalCount = Request.Form["totalchoicecount"];

            PollPE pm = Session[WebConstants.UnSavedPoll] as PollPE;
            if (pm == null)
                return RedirectToAction("create1", "Poll");

            if (pm != null)
                pm.ChoiceGroupId = int.Parse(optionVal);

            if (pm.ChoiceGroupId != 1 )
            {
                //int count = int.Parse(totalCount);
                pm.Choices = new List<ChoicePE>();
                ChoicePE cpe = null;

                for (int index = 1; index <= 10; index++)
                {
                    string choiceFromUI = Request.Form["choice" + index.ToString()];
                    if (!string.IsNullOrEmpty(choiceFromUI))
                    {
                        cpe = new ChoicePE();
                        cpe.Choice = choiceFromUI;
                        cpe.ChoiceGroupId = pm.ChoiceGroupId;
                        cpe.DateAdded = DateTime.Now;
                        cpe.DateUpdated = null;
                        pm.Choices.Add(cpe);
                    }
                    else
                        break;
                }
            }
            else
            {
                pm.Choices = new List<ChoicePE>();
                ChoicePE cpe = null;
                cpe = new ChoicePE();
                cpe.Choice = "Yes";
                cpe.ChoiceGroupId = pm.ChoiceGroupId;
                cpe.Id = 1;
                pm.Choices.Add(cpe);

                cpe = new ChoicePE();
                cpe.Choice = "No";
                cpe.ChoiceGroupId = pm.ChoiceGroupId;
                cpe.Id = 2;
                pm.Choices.Add(cpe);
            }
           Session[WebConstants.UnSavedPoll] = pm;

            return RedirectToAction("Create4", "Poll");
        }
        public ActionResult Create4()
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            PollPE ppe = Session[WebConstants.UnSavedPoll] as PollPE;
            return View(ppe);
        }

        [HttpPost]
        public ActionResult Save(HttpPostedFileBase imagefile)
        {
            if (!IsUserLoggedIn())
                return RedirectToAction("signin", "user");

            PollPE pe = Session[WebConstants.UnSavedPoll] as PollPE;
            if (pe == null)
                return RedirectToAction("create1", "Poll");

            if (imagefile != null && imagefile.ContentLength > 0)
            {
                var fileName = System.IO.Path.GetFileName(imagefile.FileName);
                var path = System.IO.Path.Combine(Server.MapPath("../content/images/uploads"), fileName);
                imagefile.SaveAs(path);
                pe.ImagePath = path;
            }
            else
            {
                pe.VideoLink = Request.Form["youtubelink"] as string;
            }

            pe.DateAdded = DateTime.Now;
            UserPE upe = Session[WebConstants.CurrentUser] as UserPE;
            PollServices.PollServicesClient peClient = new PollServices.PollServicesClient();
            peClient.AddPoll(pe, upe);

            Session[WebConstants.UnSavedPoll] = null;
            Session[WebConstants.SavedPoll] = pe;

            if (!TempData.ContainsKey(WebConstants.StatusMessage))
                TempData.Add(WebConstants.StatusMessage, "Successfully Created....");
            else
                TempData[WebConstants.StatusMessage] = "Successfully Created....";

            return RedirectToAction("myrippolls", "poll");
        }

    }
}
